#!/usr/bin/env python

#    Copyright (c) 2007, Corey Goldberg (corey@goldb.org)
#
#    This file is part of PerfLog.
#
#    PerfLog is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.


from pylab import *


def bar_graph(name_value_dict, graph_title='', output_name='bargraph.png', xlabels=True):
    figure(figsize=(4, 2)) # image dimensions  
    title(graph_title, size='small')
    
    # add bars
    name_value_dict.keys()
    for i, key in zip(range(len(name_value_dict)), name_value_dict.keys()):
        bar(i + 0.25 , name_value_dict[key], color='black')
    
    # axis setup
    xticks([]) # turn off axis labels
    if xlabels:
        xticks(arange(0.65, len(name_value_dict)),
            [('%s: %d' % (name, value)) for name, value in
            zip(name_value_dict.keys(), name_value_dict.values())], size='xx-small')
    max_value = max(name_value_dict.values())
    if max_value < 7:
        step = 1
    else:
        step = max_value / 7        
    tick_range = arange(0, max_value, step)
    yticks(tick_range, size='xx-small')
    formatter = FixedFormatter([str(x) for x in tick_range])
    gca().yaxis.set_major_formatter(formatter)
    gca().yaxis.grid(which='major')
   
    savefig(output_name) 
    